/**
 * TransferRuleCatalog.java
 * @author Daniel Liu
 * Create: 2006-12-22 ����01:21:09
 */
package org.lex.ftp.ui.prefs;

import java.awt.*;
import java.awt.event.ItemEvent;
import java.awt.event.ItemListener;
import java.util.Arrays;
import javax.swing.*;
import javax.swing.border.TitledBorder;

import org.lex.app.prefs.ResourceMap;
import org.lex.awt.ComponentUtilities;
import org.lex.ftp.FTP;
import org.lex.ftp.util.prefs.Keys;
import org.lex.swing.Swings;
import org.lex.swing.prefs.CategoryAdapter;


public class TransferRuleCategory extends CategoryAdapter {
	private static final String ASCII_FILE_DESCRIPTION = "<html><body>" + "In Auto mode all files are transferred in Binary mode unless "
			+ "the file extension is listed here.</body></html>";
	private static final String[] MODES = { "ascii", "auto", "binary" };
	private JPanel transferTypePane = null;
	private JRadioButton autoRadioButton = null;
	private JRadioButton asciiRadioButton = null;
	private JRadioButton binaryRadioButton = null;
	private JScrollPane asciiFilesScrollPane = null;
	private JList asciiFilesList = null;
	private JPanel controlPane = null;
	private JLabel asciiDescriptionLabel = null;
	private JSplitPane asciiFilesSplitPane = null;
	private JTextField textField = null;
	private JButton addButton = null;
	private JButton removeButton = null;
	private JPanel gluePane = null;
	private JRadioButton[] modes = null;

	public TransferRuleCategory() {
		super();
		initialize();
		this.modes = new JRadioButton[] { this.asciiRadioButton, this.autoRadioButton, this.binaryRadioButton };
	}

	public Component getComponent() {
		return this;
	}

	public String getTitle() {
		return "Net.Transfer Rules";
	}

	public void loadConfiguration() {
		ResourceMap rm = FTP.getContext().getResourceMap();
		String mode = rm.getString(Keys.TRANSFER_MODE);
		if (null != mode)
			this.modes[Arrays.binarySearch(MODES, mode)].setSelected(true);
		String[] asciiFiles = rm.getString(Keys.ASCIIFiles, "").split(",");
		this.getAsciiFilesList().setListData(asciiFiles);
	}

	public void saveConfiguration() {
		ResourceMap config = FTP.getContext().getResourceMap();
		String mode = null;
		for (int i = 0; i < modes.length; i++)
			if (true == modes[i].isSelected())
				mode = MODES[i];
		config.putProperty(Keys.TRANSFER_MODE, mode);
		// =================================================
		StringBuilder buffer = new StringBuilder();
		ListModel model = this.asciiFilesList.getModel();
		int count = model.getSize();
		for (int i = 0; i < count; i++)
			buffer.append(model.getElementAt(i)).append(',');
		if (buffer.length() > 0)
			buffer.setLength(buffer.length() - 1);
		config.putProperty(Keys.ASCIIFiles, buffer.toString());
	}

	// =======================
	private void initialize() {
		GridBagConstraints gridBagConstraints8 = new GridBagConstraints();
		gridBagConstraints8.fill = GridBagConstraints.BOTH;
		gridBagConstraints8.weighty = 1.0;
		gridBagConstraints8.gridx = 0;
		gridBagConstraints8.gridy = 1;
		gridBagConstraints8.weightx = 1.0;
		GridBagConstraints gridBagConstraints = new GridBagConstraints();
		gridBagConstraints.gridx = 0;
		gridBagConstraints.fill = GridBagConstraints.HORIZONTAL;
		gridBagConstraints.weightx = 1.0;
		gridBagConstraints.gridy = 0;
		this.setSize(400, 300);
		this.setLayout(new GridBagLayout());
		this.add(getTransferTypePane(), gridBagConstraints);
		this.add(getAsciiFilesSplitPane(), gridBagConstraints8);
	}

	private JPanel getTransferTypePane() {
		if (transferTypePane == null) {
			GridBagConstraints gridBagConstraints3 = new GridBagConstraints();
			gridBagConstraints3.gridx = 0;
			gridBagConstraints3.anchor = GridBagConstraints.WEST;
			gridBagConstraints3.fill = GridBagConstraints.HORIZONTAL;
			gridBagConstraints3.weightx = 1.0;
			gridBagConstraints3.insets = new Insets(0, 2, 0, 5);
			gridBagConstraints3.gridy = 2;
			GridBagConstraints gridBagConstraints2 = new GridBagConstraints();
			gridBagConstraints2.gridx = 0;
			gridBagConstraints2.anchor = GridBagConstraints.WEST;
			gridBagConstraints2.fill = GridBagConstraints.HORIZONTAL;
			gridBagConstraints2.weightx = 1.0;
			gridBagConstraints2.insets = new Insets(0, 2, 0, 5);
			gridBagConstraints2.gridy = 1;
			GridBagConstraints gridBagConstraints1 = new GridBagConstraints();
			gridBagConstraints1.gridx = 0;
			gridBagConstraints1.anchor = GridBagConstraints.WEST;
			gridBagConstraints1.fill = GridBagConstraints.HORIZONTAL;
			gridBagConstraints1.weightx = 1.0;
			gridBagConstraints1.insets = new Insets(0, 2, 0, 5);
			gridBagConstraints1.gridy = 0;
			transferTypePane = new JPanel();
			transferTypePane.setLayout(new GridBagLayout());
			transferTypePane.setBorder(BorderFactory.createTitledBorder(null, "Default Transfer Mode", TitledBorder.DEFAULT_JUSTIFICATION,
					TitledBorder.DEFAULT_POSITION, new Font("Dialog", Font.BOLD, 12), new Color(51, 51, 51)));
			transferTypePane.add(getAutoRadioButton(), gridBagConstraints1);
			transferTypePane.add(getAsciiRadioButton(), gridBagConstraints2);
			transferTypePane.add(getBinaryRadioButton(), gridBagConstraints3);
			// ==================================================================
			Swings.createButtonGroup(this.autoRadioButton, this.asciiRadioButton, this.binaryRadioButton);
		}
		return transferTypePane;
	}

	private JRadioButton getAutoRadioButton() {
		if (autoRadioButton == null) {
			autoRadioButton = new JRadioButton();
			autoRadioButton.setText("Auto Detect (Recommend)");
			autoRadioButton.addItemListener(new ItemListener() {
				public void itemStateChanged(ItemEvent e) {
					boolean selected = autoRadioButton.isSelected();
					ComponentUtilities.setEnabled(selected, getAsciiFilesScrollPane(), getAsciiFilesList(), getAsciiFilesSplitPane(),
							asciiDescriptionLabel, getTextField(), getAddButton(), getRemoveButton());
				}
			});
		}
		return autoRadioButton;
	}

	private JRadioButton getAsciiRadioButton() {
		if (asciiRadioButton == null) {
			asciiRadioButton = new JRadioButton();
			asciiRadioButton.setText("ASCII");
		}
		return asciiRadioButton;
	}

	private JRadioButton getBinaryRadioButton() {
		if (binaryRadioButton == null) {
			binaryRadioButton = new JRadioButton();
			binaryRadioButton.setText("Binary");
		}
		return binaryRadioButton;
	}

	private JScrollPane getAsciiFilesScrollPane() {
		if (asciiFilesScrollPane == null) {
			asciiFilesScrollPane = new JScrollPane();
			asciiFilesScrollPane.setViewportView(getAsciiFilesList());
		}
		return asciiFilesScrollPane;
	}

	private JList getAsciiFilesList() {
		if (asciiFilesList == null) {
			asciiFilesList = new JList();
		}
		return asciiFilesList;
	}

	private JPanel getControlPane() {
		if (controlPane == null) {
			GridBagConstraints gridBagConstraints9 = new GridBagConstraints();
			gridBagConstraints9.gridx = 0;
			gridBagConstraints9.fill = GridBagConstraints.BOTH;
			gridBagConstraints9.weightx = 1.0;
			gridBagConstraints9.weighty = 1.0;
			gridBagConstraints9.gridy = 4;
			GridBagConstraints gridBagConstraints7 = new GridBagConstraints();
			gridBagConstraints7.gridx = 0;
			gridBagConstraints7.anchor = GridBagConstraints.WEST;
			gridBagConstraints7.insets = new Insets(0, 6, 5, 5);
			gridBagConstraints7.gridy = 3;
			GridBagConstraints gridBagConstraints5 = new GridBagConstraints();
			gridBagConstraints5.insets = new Insets(0, 6, 5, 5);
			gridBagConstraints5.gridy = 2;
			gridBagConstraints5.anchor = GridBagConstraints.WEST;
			gridBagConstraints5.gridx = 0;
			GridBagConstraints gridBagConstraints4 = new GridBagConstraints();
			gridBagConstraints4.fill = GridBagConstraints.NONE;
			gridBagConstraints4.gridy = 1;
			gridBagConstraints4.ipadx = 100;
			gridBagConstraints4.ipady = 0;
			gridBagConstraints4.weightx = 0.0;
			gridBagConstraints4.anchor = GridBagConstraints.WEST;
			gridBagConstraints4.insets = new Insets(0, 6, 5, 5);
			gridBagConstraints4.gridx = 0;
			GridBagConstraints gridBagConstraints6 = new GridBagConstraints();
			gridBagConstraints6.gridx = 0;
			gridBagConstraints6.ipadx = 0;
			gridBagConstraints6.fill = GridBagConstraints.HORIZONTAL;
			gridBagConstraints6.weightx = 1.0;
			gridBagConstraints6.anchor = GridBagConstraints.WEST;
			gridBagConstraints6.insets = new Insets(6, 6, 5, 5);
			gridBagConstraints6.gridy = 0;
			asciiDescriptionLabel = new JLabel();
			asciiDescriptionLabel.setText(ASCII_FILE_DESCRIPTION);
			controlPane = new JPanel();
			controlPane.setLayout(new GridBagLayout());
			controlPane.add(asciiDescriptionLabel, gridBagConstraints6);
			controlPane.add(getTextField(), gridBagConstraints4);
			controlPane.add(getAddButton(), gridBagConstraints5);
			controlPane.add(getRemoveButton(), gridBagConstraints7);
			controlPane.add(getGluePane(), gridBagConstraints9);
		}
		return controlPane;
	}

	private JSplitPane getAsciiFilesSplitPane() {
		if (asciiFilesSplitPane == null) {
			asciiFilesSplitPane = new JSplitPane();
			asciiFilesSplitPane.setResizeWeight(0.5D);
			asciiFilesSplitPane.setBorder(BorderFactory.createTitledBorder(null, "ASCII Files", TitledBorder.DEFAULT_JUSTIFICATION,
					TitledBorder.DEFAULT_POSITION, new Font("Dialog", Font.BOLD, 12), new Color(51, 51, 51)));
			asciiFilesSplitPane.setLeftComponent(getAsciiFilesScrollPane());
			asciiFilesSplitPane.setRightComponent(getControlPane());
		}
		return asciiFilesSplitPane;
	}

	private JTextField getTextField() {
		if (textField == null) {
			textField = new JTextField();
			textField.setColumns(12);
		}
		return textField;
	}

	private JButton getAddButton() {
		if (addButton == null) {
			addButton = new JButton();
			addButton.setText("Add");
		}
		return addButton;
	}

	private JButton getRemoveButton() {
		if (removeButton == null) {
			removeButton = new JButton();
			removeButton.setText("Remove");
		}
		return removeButton;
	}

	private JPanel getGluePane() {
		if (gluePane == null) {
			gluePane = new JPanel();
			gluePane.setLayout(new GridBagLayout());
		}
		return gluePane;
	}
}
